//
//  HomeTableViewController.swift
//  swft_weibo
//
//  Created by mac on 15/9/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {
    
    var titleBtn : UIButton?
    var status : Status?
    var statusArray : [Status]?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置导行栏左右两边按钮
        setupNav()
        
        //设置头部标题
        setupTitleBtn()
        
        print(self.tableView)
        //注册cell
        
        if self.view.isKindOfClass(UITableView.self){
        
        self.tableView.registerClass(StatusTableViewCell.self, forCellReuseIdentifier: "cell")
            
            //加载数据
            reloadStatusData()
        }
        
        
    }
    /**
    加载数据
    */
    func reloadStatusData(){
        //调用模型中的方法直接加载数据，返回的闭包用来刷新数据
        Status.loadUserInformation { (statusArray) -> () in
            
            self.statusArray=statusArray
            
            self.tableView.reloadData()
        }
    }

    /**
    设置头部标题
    */
    private func setupTitleBtn(){
    
        let btn=TitleButtion()
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "btnTitleClick:", forControlEvents: UIControlEvents.TouchUpInside)
        titleBtn=btn
        navigationItem.titleView=btn
    }
    /**
    设置导行栏左右两边按钮
    */
    
    private func setupNav(){
    
    navigationItem.leftBarButtonItem=UIBarButtonItem .barButtonItem("navigationbar_friendattention", selectedImage: "navigationbar_friendattention_highlighted",action:"btnClick:",target:self)
        
        
        navigationItem.rightBarButtonItem=UIBarButtonItem.barButtonItem("navigationbar_pop", selectedImage: "navigationbar_pop_highlighted",action:"btnClick:",target:self)
    
    }
    /**
    导行栏左右两边按钮点击事件
    
    :param: btn <#btn description#>
    */
    func btnClick(btn:UIButton){
    
        print(btn)
    
    }
    /**
    导行栏中间标题按钮点击事件
    
    :param: btn
    */
    func btnTitleClick(btn:UIButton){
        
        titleBtn?.selected = !(titleBtn?.selected)!
        print(btn)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return statusArray?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
       let basicCell = tableView.dequeueReusableCellWithIdentifier("cell") as! StatusTableViewCell
        
//       basicCell?.textLabel?.text=statusArray![indexPath.row].text
        
        basicCell.status = statusArray![indexPath.row]
        
        return basicCell

    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return statusArray![indexPath.row].cellHight
    }
    
    override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 200
    }
   
}
